package cn.tedu.mall.product.service.impl;

import cn.tedu.mall.common.exception.CoolSharkServiceException;
import cn.tedu.mall.common.pojo.product.dto.AttributeTemplateAddNewDTO;
import cn.tedu.mall.common.pojo.product.dto.AttributeTemplateUpdateDTO;
import cn.tedu.mall.common.pojo.product.model.AttributeTemplate;
import cn.tedu.mall.common.pojo.product.model.CategoryAttributeTemplate;
import cn.tedu.mall.common.pojo.product.vo.AttributeTemplateDetailsVO;
import cn.tedu.mall.common.pojo.product.vo.AttributeTemplateListItemVO;
import cn.tedu.mall.common.pojo.product.vo.AttributeTemplateStandardVO;
import cn.tedu.mall.common.pojo.product.vo.CategoryStandardVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.ResponseCode;
import cn.tedu.mall.product.constant.DataCommonConst;
import cn.tedu.mall.product.mapper.AttributeMapper;
import cn.tedu.mall.product.mapper.AttributeTemplateMapper;
import cn.tedu.mall.product.mapper.CategoryAttributeTemplateMapper;
import cn.tedu.mall.product.mapper.CategoryMapper;
import cn.tedu.mall.product.service.IAttributeTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class AttributeTemplateServiceImpl implements IAttributeTemplateService {
    @Autowired
    private AttributeTemplateMapper attributeTemplateMapper;
    @Autowired
    private AttributeMapper attributeMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;

    @Override
    public void addNew(AttributeTemplateAddNewDTO attributeTemplateAddnewDTO) {
        //判断类别是否有效
        Long categoryId  = attributeTemplateAddnewDTO.getCategoryId();
        CategoryStandardVO categoryStandardVO = categoryMapper.getById(categoryId);
        if(categoryStandardVO == null){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"新增失败，类别不存在！");
        }
        if(categoryStandardVO.getParent() == null){
            throw new CoolSharkServiceException(ResponseCode.NOT_ACCEPTABLE,"新增失败，类别不完整！");
        }
        if(categoryStandardVO.getParent().equals(1)){
            throw new CoolSharkServiceException(ResponseCode.NOT_ACCEPTABLE,"新增失败，模板还有子集！");
        }
        //判断名称是否被占用
        String name = attributeTemplateAddnewDTO.getName();
        Object checkName = attributeTemplateMapper.getByName(name);
        if(checkName != null){
            throw new CoolSharkServiceException(ResponseCode.CONFLICT,"新增失败，名称已存在！");
        }
        //新增模板
        AttributeTemplate attributeTemplate = new AttributeTemplate();
        BeanUtils.copyProperties(attributeTemplateAddnewDTO,attributeTemplate);
        attributeTemplate.setSort(attributeTemplateAddnewDTO.getSort()==null? DataCommonConst.SORT_DEFAULT:attributeTemplateAddnewDTO.getSort());
        int rows = attributeTemplateMapper.insert(attributeTemplate);
        if(rows != 1){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"新增失败，稍后再试！");
        }
        //增加类型与属性模板的关联数据
        CategoryAttributeTemplate categoryAttributeTemplate = new CategoryAttributeTemplate();
        categoryAttributeTemplate.setCategoryId(attributeTemplateAddnewDTO.getCategoryId());
        categoryAttributeTemplate.setAttributeTemplateId(attributeTemplate.getId());
        int rows1 = categoryAttributeTemplateMapper.insert(categoryAttributeTemplate);
        if(rows1 != 1){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"新增失败，稍后再试！");
        }
    }

    @Override
    public void deleteById(Long id) {
        //检查要删除的模板是否存在
        Object attributeTemplate = attributeTemplateMapper.getById(id);
        if(attributeTemplate == null){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"删除失败，模板不存在！");
        }
        //检查是否关联商品属性
        int count = attributeMapper.countByTemplateId(id);
        if(count>0){
            throw new CoolSharkServiceException(ResponseCode.CONFLICT,"删除失败，有关联的商品属性！");
        }
        //删除
        int rows = attributeTemplateMapper.deleteById(id);
        if(rows!=1){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"删除失败，稍后再试！");
        }
    }

    @Override
    public void updateById(Long id, AttributeTemplateUpdateDTO attributeTemplateUpdateDTO) {
        log.info("-------------修改模板数据----------");
        //检查要修改的模板是否存在
        Object attributeTemplate = attributeTemplateMapper.getById(id);
        if(attributeTemplate == null){
            throw new CoolSharkServiceException(ResponseCode.NOT_FOUND,"修改失败，模板不存在！");
        }
        //检查更新名称是否已存在
        AttributeTemplateStandardVO attributeTemplateStandardVO = attributeTemplateMapper.getByName(attributeTemplateUpdateDTO.getName());
        if(attributeTemplateStandardVO!=null && !attributeTemplateStandardVO.getId().equals(id)){
            throw new CoolSharkServiceException(ResponseCode.CONFLICT,"修改失败，模板名称已存在！");
        }
        //更新
        AttributeTemplate attributeTemplateNew = new AttributeTemplate();
        attributeTemplateNew.setId(id);
        BeanUtils.copyProperties(attributeTemplateUpdateDTO,attributeTemplateNew);
        int rows = attributeTemplateMapper.update(attributeTemplateNew);
        if(rows!=1){
            throw new CoolSharkServiceException(ResponseCode.INTERNAL_SERVER_ERROR,"修改失败，稍后重试！");
        }
    }

    @Override
    public AttributeTemplateDetailsVO getDetailsById(Long id) {
        AttributeTemplateDetailsVO attributeTemplateDetailsVO = attributeTemplateMapper.getDetailsById(id);
        return attributeTemplateDetailsVO;
    }

    @Override
    public AttributeTemplateDetailsVO getDetailsIncludeSaleAttributeById(Long id) {
        AttributeTemplateDetailsVO attributeTemplateDetailsVO = attributeTemplateMapper.getDetailsByIdAndAttributeType(id, 1);
        return attributeTemplateDetailsVO;
    }

    @Override
    public AttributeTemplateDetailsVO getDetailsIncludeNonSaleAttributeById(Long id) {
        AttributeTemplateDetailsVO attributeTemplateDetailsVO = attributeTemplateMapper.getDetailsByIdAndAttributeType(id, 0);
        return attributeTemplateDetailsVO;
    }

    @Override
    public JsonPage<AttributeTemplateStandardVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<AttributeTemplateStandardVO> lists = attributeTemplateMapper.list();
        return JsonPage.restPage(new PageInfo<>(lists));
    }

    @Override
    public List<AttributeTemplateListItemVO> listByCategoryId(Long categoryId) {
        List<AttributeTemplateListItemVO> list = attributeTemplateMapper.listByCategoryId(categoryId);
        return list;
    }
}
